﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.BussinessLogic;
using QLTV_V2.BussinessObject;
using QLTV_V2.DataAccess;

namespace QLTV_V2
{
    public partial class FrmBanDoc : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public FrmBanDoc()
        {
            InitializeComponent();
        }
        ConnectData conn = new ConnectData();
        BanDocBUS bdBUS=new BanDocBUS();
       // NhomBanDocBUS nbdBUS = new NhomBanDocBUS();
        Utilities uti = new Utilities();
        
        private void FrmBanDoc_Load(object sender, EventArgs e)
        {
            //Load Gioi tinh
            comb_GioiTinh.Items.Add("Nam");
            comb_GioiTinh.Items.Add("Nữ");
            comb_GioiTinh.SelectedIndex = 0;
            //Load combobox nhom ban doc
            comb_MaNhom.DataSource = uti.getDataSource("NhomBanDoc", "TenNhom");
            comb_MaNhom.DisplayMember = "TenNhom";
            comb_MaNhom.ValueMember = "MaNhom";
            //Load trang thai the
            comb_TTThe.Items.Add("Kích hoạt");
            comb_TTThe.Items.Add("Hết hạn");
            comb_TTThe.Items.Add("Bị khóa");
            comb_TTThe.SelectedIndex = 0;
            //Load datagridView
            dgv_BanDoc.DataSource = uti.getDataSource("BanDoc", "MaBanDoc");
            //
            int k = dgv_BanDoc.CurrentCell.RowIndex+1;
            int dem = dgv_BanDoc.Rows.Count - 1;
            lbl_Show.Text = k.ToString() + " / " + dem.ToString();
        }
        private void dataGridView_Bandoc_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int dong = e.RowIndex;
                txt_MaBanDoc.Text = dgv_BanDoc.Rows[dong].Cells["MaBanDoc"].Value.ToString();
                txt_TenBandoc.Text = dgv_BanDoc.Rows[dong].Cells["TenBanDoc"].Value.ToString();
                if (dgv_BanDoc.Rows[dong].Cells["NgaySinh"].Value.ToString() == "")
                    date_Ngaysinh.ResetText();
                else
                    date_Ngaysinh.Text = dgv_BanDoc.Rows[dong].Cells["NgaySinh"].Value.ToString();
                if (dgv_BanDoc.Rows[dong].Cells["GioiTinh"].Value.ToString() == "True")
                    comb_GioiTinh.SelectedItem = "Nam";
                else
                    comb_GioiTinh.SelectedItem = "Nữ";
                txt_Diachi.Text = dgv_BanDoc.Rows[dong].Cells["DiaChi"].Value.ToString();
                txt_Email.Text = dgv_BanDoc.Rows[dong].Cells["Email"].Value.ToString();
                txt_SoDT.Text = dgv_BanDoc.Rows[dong].Cells["SoDT"].Value.ToString();
                date_NgayLap.Text = dgv_BanDoc.Rows[dong].Cells["NgayLapThe"].Value.ToString();
                date_NgayHet.Text = dgv_BanDoc.Rows[dong].Cells["NgayHetHan"].Value.ToString();
                comb_MaNhom.SelectedValue = dgv_BanDoc.Rows[dong].Cells["MaNhom"].Value.ToString();
                txt_Anhcanhan.Text = dgv_BanDoc.Rows[dong].Cells["AnhCaNhan"].Value.ToString();
                pictureBox1.BackgroundImage = System.Drawing.Image.FromFile(txt_Anhcanhan.Text, true);
                comb_TTThe.SelectedItem = dgv_BanDoc.Rows[dong].Cells["TTThe"].Value.ToString();
                /////////////
                int dem = dgv_BanDoc.Rows.Count - 1;
                lbl_Show.Text = (dong + 1).ToString() + " / " +dem.ToString();
            }
            catch
            {
                
            }
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            FrmLaptheBandoc frm = new FrmLaptheBandoc();
            frm.kt = 1;
            frm.ShowDialog();
            FrmBanDoc_Load(sender, e);
        }
        
        private void btn_NhapDSBandoc_Click(object sender, EventArgs e)
        {
            Frm_NhapDSbandoc frm = new Frm_NhapDSbandoc();
            frm.ShowDialog();
            FrmBanDoc_Load(sender, e);
        }
        
        private void btn_Sua_Click(object sender, EventArgs e)
        {
            FrmLaptheBandoc frm = new FrmLaptheBandoc();
            frm.kt = 2;
            frm.mabandoc = txt_MaBanDoc.Text;
            frm.ShowDialog();
            FrmBanDoc_Load(sender, e);
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            int count=dgv_BanDoc.Rows.Count-1;
            dgv_BanDoc.CurrentCell = dgv_BanDoc.Rows[count].Cells[0];
            dgv_BanDoc.CurrentCell = dgv_BanDoc.Rows[0].Cells[0];
            int dem=0;
            int chon = 0;
            if (MessageBox.Show("Kiểm tra thông tin trước khi xóa?", "Hỏi",
              MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                for (int i = 0; i < dgv_BanDoc.Rows.Count; i++)
                {
                    if (Convert.ToBoolean(dgv_BanDoc.Rows[i].Cells["check"].Value) == true)
                    {
                        chon++;
                        string mabandoc = dgv_BanDoc.Rows[i].Cells["MaBanDoc"].Value.ToString();
                        if (MessageBox.Show("Bạn có muốn xóa " + mabandoc + " không?", "Hỏi",
              MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            try
                            {
                                if (uti.CheckExitsValue("PhieuMuonTra", "MaBanDoc", mabandoc) == true)//Không tồn tại
                                {
                                    if (uti.deleteTable("BanDoc", "MaBanDoc", mabandoc) == true)
                                        dem++;
                                }
                                else
                                {
                                    if (MessageBox.Show("Thông tin " + mabandoc + "hiện đang lưu trong bảng khác. Xóa không ?", "Hỏi",
                                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                    {
                                        if (uti.deleteTable("BanDoc", "MaBanDoc", mabandoc) == true)
                                            dem++;
                                    }
                                }
                            }
                            catch
                            {
                                MessageBox.Show("Không thể xóa" + mabandoc);
                            }
                        }
                    }
                }
            }
            else //Không xuất hiện hộp thoại
            {
                for (int i = 0; i < dgv_BanDoc.Rows.Count; i++)
                {
                    if (Convert.ToBoolean(dgv_BanDoc.Rows[i].Cells["check"].Value) == true)
                    {
                        chon++;
                        string mabandoc = dgv_BanDoc.Rows[i].Cells["MaBanDoc"].Value.ToString();
                        if (uti.deleteTable("BanDoc", "MaBanDoc", mabandoc) == true)
                            dem++;
                    }
                }
            }
           
            MessageBox.Show("Đã xóa " + dem + "/" + chon + " thẻ được chọn!", "Thông báo");
            FrmBanDoc_Load(sender, e);
        }

        private void btn_GiaHan_Click(object sender, EventArgs e)
        {
            int dong = dgv_BanDoc.CurrentCell.RowIndex;
            FrmGiahanThe frm = new FrmGiahanThe();
            frm.mabandoc = dgv_BanDoc.Rows[dong].Cells["MaBanDoc"].Value.ToString();
            frm.ShowDialog();
            FrmBanDoc_Load(sender, e);
        }

        private void btn_Inthe_Click(object sender, EventArgs e)
        {
            
            int sum = dgv_BanDoc.Rows.Count - 1;
            dgv_BanDoc.CurrentCell = dgv_BanDoc.Rows[sum].Cells[0];
            dgv_BanDoc.CurrentCell = dgv_BanDoc.Rows[0].Cells[0];
            DataTable dtb = new DataTable();
            DataRow newRow = null;
            int dem = 0;
            dtb.Columns.Add(new DataColumn("MaBanDoc", typeof(string)));
            dtb.Columns.Add(new DataColumn("TenBanDoc", typeof(string)));
            dtb.Columns.Add(new DataColumn("TenNhom", typeof(string)));
            dtb.Columns.Add(new DataColumn("NgayLapThe", typeof(DateTime)));
            dtb.Columns.Add(new DataColumn("NgayHetHan", typeof(DateTime)));
            for (int i = 0; i < dgv_BanDoc.Rows.Count; i++)
            {
                if (Convert.ToBoolean(dgv_BanDoc.Rows[i].Cells["check"].Value) == true)
                {
                    dem++;
                    newRow = dtb.NewRow();
                    newRow["MaBanDoc"] = dgv_BanDoc.Rows[i].Cells["MaBanDoc"].Value.ToString();
                    newRow["TenBanDoc"] = dgv_BanDoc.Rows[i].Cells["TenBanDoc"].Value.ToString();
                    newRow["TenNhom"] = bdBUS.GetTenNhom(dgv_BanDoc.Rows[i].Cells["MaNhom"].Value.ToString());
                    newRow["NgayLapThe"] = Convert.ToDateTime(dgv_BanDoc.Rows[i].Cells["NgayLapThe"].Value);
                    newRow["NgayHetHan"] = Convert.ToDateTime(dgv_BanDoc.Rows[i].Cells["NgayHetHan"].Value);
                    dtb.Rows.Add(newRow);
                }
            }
            if (dem != 0)
            {
                frmReportBanDoc frm = new frmReportBanDoc();
                frm.dtb_BanDoc = dtb;
                frm.dk = 1;
                frm.ShowDialog();
                int count = dgv_BanDoc.Rows.Count - 1;
                for (int i = 0; i < count; i++)
                    dgv_BanDoc.Rows[i].Cells["check"].Value = false;
            }
            else
                MessageBox.Show("Chưa chọn mã thẻ!", "Thông báo");
        }

        #region Xử lý di chuyển
        private void btn_Left_end_Click(object sender, EventArgs e)
        {
            int dem = dgv_BanDoc.Rows.Count - 1;
            dgv_BanDoc.CurrentCell = dgv_BanDoc.Rows[0].Cells[0];
            lbl_Show.Text = "1 / " + dem.ToString();
        }
       
        private void btn_Left_Click(object sender, EventArgs e)
        {
            try
            {
                int k = dgv_BanDoc.CurrentCell.RowIndex;
                int dem = dgv_BanDoc.Rows.Count - 1;
                dgv_BanDoc.CurrentCell = dgv_BanDoc.Rows[k - 1].Cells[0];
                lbl_Show.Text = k + " / " + dem.ToString();
            }
            catch
            {
                lbl_Show.Text = "1 / " + (dgv_BanDoc.Rows.Count - 1).ToString();
            }
        }

        private void btn_Right_Click(object sender, EventArgs e)
        {
            try
            {
                int k = dgv_BanDoc.CurrentCell.RowIndex;
                int dem = dgv_BanDoc.Rows.Count - 1;
                dgv_BanDoc.CurrentCell = dgv_BanDoc.Rows[k + 1].Cells[0];
                lbl_Show.Text = (k + 2) + " / " + dem.ToString();
            }
            catch
            {
                string k = (dgv_BanDoc.Rows.Count - 1).ToString();
                lbl_Show.Text = k + " / " + k;
            }
        }

        private void btn_Right_Left_Click(object sender, EventArgs e)
        {
            int dem = dgv_BanDoc.Rows.Count - 1;
            dgv_BanDoc.CurrentCell = dgv_BanDoc.Rows[dem - 1].Cells[0];
            lbl_Show.Text = dem.ToString() + " / " + dem.ToString();
        }
        #endregion
     
        private void dgv_BanDoc_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            uti.dgv_GetSTT_RowHeader(sender, e, dgv_BanDoc);
        }


    }
}
